package com.cinema.servlet;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cinema.Bean.Admin;
import com.cinema.Bean.Hall;
import com.cinema.dao.IAdminDao;
import com.cinema.dao.imp.AdminDaoMysql;

public class AdminServlet extends HttpServlet {
    
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	private IAdminDao iAdminDao = new AdminDaoMysql();
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String method = request.getParameter("method");
		// 用户新增
		if ("add".equals(method)) {
			String adminId = request.getParameter("adminId");
			String adminName = request.getParameter("adminName");
			String adminPwd = getMD5(request.getParameter("adminPwd"));
			Admin u = new Admin();
			u.setAdminId(adminId);
			u.setAdminName(adminName);
			u.setAdminPwd(adminPwd);

			int insert = iAdminDao.insert(u);

			if (insert == 1) {
				
				request.getSession().removeAttribute("alist");

			}
			response.sendRedirect("Admin/index.jsp");
		} else if("delete".equals(method)) {
			String adminId = request.getParameter("adminId");
			if (adminId != null && adminId != "") {
				
				Admin admin = new Admin();
				admin.setAdminId(adminId);

				int delete = iAdminDao.delete(admin);

				if (delete == 1) {
					//response.getOutputStream().print("success");
					response.getWriter().print("success"); 
					request.getSession().removeAttribute("alist");
				}else{
					
					response.getWriter().print("error");
				}
			}
		}else if("getAdmin".equals(method)){
			String adminId = request.getParameter("adminId");
			
			if(adminId!=null && adminId!=""){
				Admin admin = iAdminDao.SelectById(adminId);
				request.setAttribute("admin", admin);
			}
			request.getRequestDispatcher("/Admin/edit.jsp").forward(request, response);
			
		}else if("update".equals(method)){
			String adminId = request.getParameter("adminId");
			String adminName = request.getParameter("adminName");
			String adminPwd = getMD5(request.getParameter("adminPwd"));
			Admin admin=new Admin(adminId, adminName, adminPwd);
			int update = iAdminDao.update(admin);
			if(update==1){
				request.getSession().removeAttribute("alist");
			}
			response.sendRedirect("Admin/index.jsp");
		}else if("select".equals(method)){
			request.getSession().removeAttribute("alist");
			String keyword=request.getParameter("keyword");
			
			List<Admin> admin=iAdminDao.Select(keyword);
			request.getSession().setAttribute("alist", admin);
		}
	}
	
	/** 
     * 对密码md5加密(大写+数字) 
     * @param str 传入要加密的字符串 
     * @return  MD5加密后的字符串 
     */  
    public static String getMD5(String s) {  
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};         
  
        try {  
            byte[] btInput = s.getBytes();  
            // 获得MD5摘要算法的 MessageDigest 对象  
            MessageDigest mdInst = MessageDigest.getInstance("MD5");  
            // 使用指定的字节更新摘要  
            mdInst.update(btInput);  
            // 获得密文  
            byte[] md = mdInst.digest();  
            // 把密文转换成十六进制的字符串形式  
            int j = md.length;  
            char str[] = new char[j * 2];  
            int k = 0;  
            for (int i = 0; i < j; i++) {  
                byte byte0 = md[i];  
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];  
                str[k++] = hexDigits[byte0 & 0xf];  
            }  
            return new String(str);  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
    }

}
